<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>收货地址</title>
<%@ include file="/page/wx/include.jsp"%>
<script type="text/javascript" src="<%=basePath%>/statics/plugins/layer/layer.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>/statics_wx/css/style.css">
</head>
<body style="background: #f5f5f5">
	<c:choose>
		<c:when test="${empty addressList }">
			<div class="noresult">
				<p>
					<i><img src="<%=basePath%>/statics_wx/images/icon_noaddress.png" /></i> <span>您还没有收货地址~</span>
				</p>
			</div>
		</c:when>
		<c:otherwise>
			<div class="sh_address_list1">
				<ul>
					<c:forEach items="${addressList }" var="address">
						<li>
							<div class="box14">
								<div class="box14_con" onclick="javascript: doSelectAddress('${address.id}');">
									<h2>
										<strong>${address.receiverName}</strong> <span>${address.mobile}</span>
									</h2>
									<p>${address.provinceName}${address.cityName}${address.areaName}${address.streetName}${address.address}</p>
								</div>
								<h3>
									<c:choose>
										<c:when test="${address.isDefault == 1 }">
											<label class="current"><strong><i></i><span>默认地址</span></strong></label>
										</c:when>
										<c:otherwise>
											<label onclick="javascript: setDefault('${address.id}');"><strong><i></i><span>设为默认地址</span></strong></label>
										</c:otherwise>
									</c:choose>
									<b> <a href="javascript: doEditAddressConfirm('${address.id}');"><i class="icon1"></i><span>编辑</span></a> <a
										href="javascript: doDelAddressConfirm('${address.id}');"><i class="icon2"></i><span>删除</span></a>
									</b>
								</h3>
							</div>
						</li>
					</c:forEach>
				</ul>
			</div>
		</c:otherwise>
	</c:choose>
	<div class="footerfixed4"></div>
	<div class="add_btn">
		<p>
			<input type="button" class="inputbtn1" value="新 增" onclick="javascript: doNewAddress();" />
		</p>
	</div>

	<div class="reg_tips" style="display: none">
		<p>设置成功</p>
	</div>
	
	<div class="overlay" style="display: none" id="del_box">
		<div class="msg_box">
			<div class="bd">
				<p>
					确认删除此地址？<input type="hidden" id="addressId" />
				</p>
			</div>
			<div class="bottom">
				<ul>
					<li><a href="javascript: $('#del_box').hide();">取消</a><i></i></li>
					<li><a href="javascript: doDelAddress();">确认</a></li>
				</ul>
			</div>
		</div>
	</div>
	
	<div class="overlay" style="display: none" id="edit_box">
		<div class="msg_box">
			<div class="bd">
				<p>
					该地址为免费地址，修改后需重新审核，确认修改？
				</p>
			</div>
			<div class="bottom">
				<ul>
					<li><a href="javascript: $('#edit_box').hide();">取消</a><i></i></li>
					<li><a href="javascript: doEditAddress()">确认</a></li>
				</ul>
			</div>
		</div>
	</div>
	<input type="hidden"  id="shopping_ids" value="${shoppingIds}"/>
<script type="text/javascript">

	function doSelectAddress(addressId) {
		var shopping_ids = $("#shopping_ids").val();
		if(shopping_ids != null && shopping_ids != ""){
			var url = rootPath + "/wx/shoppingcart/settlement";
			var form = $('<form></form>');  
		    form.attr('action', url);  
		    form.attr('method', 'post');
		    form.attr('target', '_self');
		    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
		    form.append("<input type='hidden' name='addressId' value='"+addressId+"'/>");
		    form.appendTo("body");
		    form.submit();
		}
	}

	/**
	 * 设置默认地址
	 */
	function setDefault(addressId) {
		if (addressId) {
			$.ajax({
				type : "post",
				url : rootPath + '/wx/address/doSetDefault',
				data : {
					addressId : addressId
				},
				async : false,
				datatype : "json",
				success : function(json) {
					if (json.code == '1') {
						// layer.msg(json.msg);
						var shopping_ids = $("#shopping_ids").val();
						var url = rootPath + "/wx/address/toWxAddressListPage";
						var form = $('<form></form>');  
					    form.attr('action', url);  
					    form.attr('method', 'post');
					    form.attr('target', '_self');
					    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
					    form.appendTo("body");
					    form.submit();
					    
						/*
						layer.msg(json.msg, function() {
							var shopping_ids = $("#shopping_ids").val();
							var url = rootPath + "/wx/address/toWxAddressListPage";
							var form = $('<form></form>');  
						    form.attr('action', url);  
						    form.attr('method', 'post');
						    form.attr('target', '_self');
						    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
						    form.appendTo("body");
						    form.submit();
						}, {
							time : 500
						});
						*/
					} else {
						layer.msg(json.msg, {
							time : 2000
						});
					}
				},
				error : function() {
					layer.msg('设置默认地址失败', {
						time : 1000
					});
				}
			});
		} else {
			layer.msg('请先选择地址', {
				time : 1000
			});
		}
	}

	/**
	 * 修改地址-确认框
	 */
	function doEditAddressConfirm(addressId) {
		$("#addressId").val(addressId);
		if (addressId != "" && addressId != null) {
			//该地址是否是免费地址
			$.ajax({
				type: 'POST',
				url: rootPath + "/deliveryAddress/isFreeExpressAddress",
				data:{addressId: addressId},
				dataType: 'json',
				success: function(data){
					if (data.resultCode == "1") {
						$('#edit_box').show();
					} else {
						doEditAddress();
					}
				}
			});
		}
	}
	/**
	 * 转编辑页面
	 */
	function doEditAddress() {
		var shopping_ids = $("#shopping_ids").val();
		var addressId = $("#addressId").val();
		var url = rootPath + "/wx/address/toWxAddressEditPage";
		var form = $('<form></form>');  
	    form.attr('action', url);  
	    form.attr('method', 'post');
	    form.attr('target', '_self');
	    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
	    form.append("<input type='hidden' name='addressId' value='"+addressId+"'/>");
	    form.appendTo("body");
	    form.submit();
	}
	/**
	 * 转新增页面
	 */
	function doNewAddress() {
		var shopping_ids = $("#shopping_ids").val();
		var url = rootPath + "/wx/address/toWxAddressEditPage";
		var form = $('<form></form>');  
	    form.attr('action', url);  
	    form.attr('method', 'post');
	    form.attr('target', '_self');
	    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
	    form.appendTo("body");
	    form.submit();
	}

	/**
	 * 删除地址-确认框
	 */
	function doDelAddressConfirm(addressId) {
		$("#addressId").val(addressId);
		$('#del_box').show();
	}
	/**
	 * 删除地址
	 */
	function doDelAddress() {
		var addressId = $("#addressId").val();
		var shopping_ids = $("#shopping_ids").val();
		var url = rootPath + "/wx/address/doDelAddress";
		var form = $('<form></form>');  
	    form.attr('action', url);  
	    form.attr('method', 'post');
	    form.attr('target', '_self');
	    form.append("<input type='hidden' name='shoppingIds' value='"+shopping_ids+"'/>");
	    form.append("<input type='hidden' name='addressId' value='"+addressId+"'/>");
	    form.appendTo("body");
	    form.submit();
	}
</script>
</body>
</html>